local redis = require "resty.redis"

--打开redis连接
local function open_redis()
    local red = redis:new()
    red:set_timeout(1000) -- 超时时间1 second
    local res = red:connect('192.168.0.128',6379)
    if not res then
        return nil
    end
        res = red:auth(123456)  --密码校验
        if not res then
            return nil
        end
    red.close = close
    return red
end

--关闭连接
local function close(self)
    local sock = self.sock
    if not sock then
        return nil, "not initialized"
    end
    if self.subscribed then
        return nil, "subscribed state"
    end
    return sock:setkeepalive(10000, 50)
end

local key =  'name'
local val =  "100"
local arg = ngx.req.get_uri_args() --取req里所有的参数
for k,v in pairs(arg) do
   key = k
   val = v
   break;
end

local red = open_redis()
--local value = red:get(key)  --取值
--red:set(key,val)	    --设新值
--close(red)

red:init_pipeline()
value = red:get(key)
red:set(key, val)
red:commit_pipeline()

--返回值到页面
ngx.say(key,':',value)
